<template>
  <el-row :gutter="20">
    <el-col :span="12">
      <div class="home_orders"><home_orders /></div>
    </el-col>
    <el-col :span="12">
      <div v-if="!loginSuccess" class="home_login">
        <div class="shell">
          <!-- <div class="left"></div> -->
          <div class="right">
            <h4>We are<span> STW </span></h4>
            <!-- <p>Welcome to your arrival,login to your accout to enter the page:</p> -->
            <div class="email">
              <i class="iconfont icon-email"></i>
              <input type="name" v-model="phone" placeholder="  手机号" />
            </div>
            <div class="password">
              <i class="iconfont icon-email"></i>
              <input type="password" v-model="password" placeholder="  密码" />
            </div>
            <div class="check">
              <div>
                <img :src="tempImageURL" alt="Temporary Image" />

                <button class="aaa" @click="refreshImage">
                  <el-icon><RefreshLeft /></el-icon>
                </button>
              </div>
            </div>
            <div class="email">
              <input
                class="input"
                v-model="verification_code"
                placeholder="  验证码"
              />
            </div>
            <div class="btnLogin">
              <button @click="login">Log in</button>
            </div>
            <a class="aaa" @click="btnRegister">Go to Register</a>
          </div>
        </div>
      </div>
      <div v-else class="success"><h3>你好啊</h3></div>
    </el-col>
  </el-row>
</template>

<script>
import axios from "axios";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";
import home_orders from "@/components/home_orders.vue";
// import home_login from "@/components/home_login.vue";

export default {
  computed: {
    userId() {
      return this.store.state.user_id;
    },
  },
  data() {
    return {
      loginSuccess: false,
      store: useStore(),
      phone: "",
      password: "",
      tempImageURL: "",
      code: "",
      login_data: "",
    };
  },
  created() {
    this.refreshImage();
    // console.log(this.userId);
  },
  methods: {
    btnRegister() {
      this.$router.push({
        path: "/RegisterPage",
      });
    },
    refreshImage() {
      // 发送请求获取图片比特流数据
      // 这里假设使用axios库发送请求
      axios
        .get("http://127.0.0.1:8000/image_code/", {
          responseType: "arraybuffer",
        })
        .then((response) => {
          // 创建Blob对象
          const blob = new Blob([response.data], { type: "image/png" });
          // 生成临时图片URL
          this.tempImageURL = URL.createObjectURL(blob);
          // console.log(response);
          const code = response["clear"];
          console.log("Image code:", code);
        })
        .catch((error) => {
          console.error(error);
        });
    },
    handleChange(newUserId) {
      this.store.dispatch("change", newUserId);
      // console.log(this.store.state.user_id);
    },
    login() {
      const params = new URLSearchParams();
      params.append("phone", this.phone);
      // console.log("aaa", this.user_name);
      params.append("password", this.password);
      params.append("verification_code", this.verification_code);
      // // console.log('aaa');
      this.axios.post("http://127.0.0.1:8000/log_in/", params).then((res) => {
        if (res.data.success) {
          this.loginSuccess = true;
          ElMessage({
            message: "登陆成功",
            type: "success",
          });
          // window.alert("window.alert(res.data.data);");
          // this.login_data = res.data.data;
          this.handleChange(res.data.data);
        } else {
          ElMessage.error(res.data.data);
          // window.alert(res.data.data);
        }
        // this.login_data = res.data.data;
        // window.alert(res.data.data);
        //Result(success,msg,data)
        // // console.log(id);
        // console.log(res.data.data);
        // console.log(this.userId);
      });
    },
  },
  beforeUnmount() {
    // 在组件卸载前，释放临时图片URL资源
    URL.revokeObjectURL(this.tempImageURL);
  },
  components: {
    // home_login,
    home_orders,
  },
};
</script>

<style>
* {
  padding: 0;
  margin: 0;
}

.shell {
  display: flex;
  height: 435px;
  background-color: rgb(255, 255, 255);
  box-shadow: 0px 2px 6px -1px rgba(0, 0, 0, 0.3);
  border-radius: 3px;
}
.left {
  width: 220px;
  height: auto;
  /* background-image: url(); */
  background-color: #aba0e6;
  background-size: cover;
}
.right {
  right: 80vh;
  width: 300px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 40px 30px;
}
.right h4 {
  font-size: 24px;
  color: ərgba(0, 0, 0, 0, 0.4);
  margin-bottom: 20px;
}
.right span {
  color: #aba0e6;
}
.right p {
  max-width: 220px;
  font: 14px;
  margin-bottom: 40px;
}
.email,
.password {
  display: flex;
  height: 50px;
  width: 100%;
}
.iconfont {
  font-size: 28px;
  line-height: 56px;
  margin-right: 5px;
  color: #d8d8d8;
}

.right input {
  border: none;
  font-size: 16px;
  width: 100%;
  outline: none;
  border-bottom: #aba0e6 1px dotted;
}

.btnLogin {
  display: flex;
  justify-content: flex-end;
}
.btnLogin button {
  min-width: 100px;
  border-radius: 24px;
  text-align: center;
  padding: 15px 40px;
  background-color: #b08bf8;
  color: #fff;
  font-size: 14px;
  margin-left: auto;
  font-weight: 500;
  box-shadow: 0px 2px 6px -1px rgba(0, 0, 0, 0.13);
  border: none;
  margin-top: 30px;
}
.btnLogin button:hover {
  cursor: pointer;
}
.check {
  padding: 10px;
  display: flex;
  justify-content: space-around;
}
.check button {
  margin-left: 20px;
  margin-top: 0px;
  border-radius: 24px;
  text-align: center;
  padding: 8px 10px;
  background-color: #b08bf8;
  color: #fff;
  box-shadow: 0px 2px 6px -1px rgba(0, 0, 0, 0.13);
  border: none;
}
.right a {
  text-align: right;
  font-size: 10px;
  color: #aba0e6;
  margin-top: 30px;
}

.aaa:hover {
  cursor: pointer;
}
.success {
  display: flex;
}
body {
  /* background-color: rgba(235, 235, 224, 0.722); */
  margin: 0px;
  padding: 0%;
}

/* 定义全局变量 */
:root {
  --rotate-speed: 40;
  --count: 8;
  --easing: cubic-bezier(0, 0.37, 1, 0.63);
}

/* 设置页面样式 */
.home_login {
  border-radius: 15px;
  margin-top: 10vh;
  margin-right: 20vh;
  margin-left: 25vh;
  animation: huxi 5s linear infinite alternate;
}

@keyframes huxi {
  0% {
    box-shadow: 0px 0px 30px 30px rgb(211, 194, 246);
  }

  25% {
    box-shadow: 0px 0px 30px 30px rgb(222, 210, 250);
  }

  50% {
    box-shadow: 0px 0px 30px 30px rgb(225, 214, 250);
  }

  75% {
    box-shadow: 0px 0px 30px 30px rgb(235, 229, 253);
  }

  100% {
    box-shadow: 0px 0px 30px 30px rgb(250, 249, 254);
  }
}

.home_orders {
  top: 10vh;
}

.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}
</style>
